package com.think.archive.service.impl;

import com.think.archive.mapper.InfoFileMapper;
import com.think.archive.model.InfoFile;
import com.think.archive.service.InfoFileService;
import com.think.archive.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class InfoFileServiceImpl implements InfoFileService {

    @Autowired
    InfoFileMapper fileMapper;

    @Value("${uploadFile.location}")
    private String uploadFileLocation;
    @Value("${uploadFile.resourceHandler}")
    private String resourceHandler;

    @Override
    public int insert(InfoFile file) throws Exception {
        try {
            int res=fileMapper.insert(file);
            if(res>0){
                return res;
            }else{
                throw new Exception("保存失败，请稍后再试");
            }
        }
        catch (Exception e){
            throw e;
        }
    }

    @Override
    public InfoFile selectByGuid(String f_guid) throws Exception {
        try {
            InfoFile file=fileMapper.selectByGuid(f_guid);
            if(file!=null){
                return file;
            }else{
                throw new Exception("暂无数据");
            }
        }
        catch (Exception e){
            throw e;
        }
    }


    @Override
    public int insertUpdateFile(HttpServletRequest request, MultipartFile file,String archiveId) throws Exception {
        String uuid= CommonUtil.getSimpleUUID();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        String fileName = file.getOriginalFilename();
        String[] f=fileName.split("\\.");
        String file_end="jpg";
        if(!"blob".equals(f[f.length-1])){
            file_end=f[f.length-1];
        }
        String fileServerPath = basePath + resourceHandler.substring(0, resourceHandler.lastIndexOf("/") + 1) + uuid+"."+file_end;
        File saveFile = new File(uploadFileLocation, uuid +"."+file_end);
        if(!saveFile.exists()){
            saveFile.mkdirs();
        }
        file.transferTo(saveFile);
        InfoFile myFile=new InfoFile();
        myFile.setFGuid(uuid);
        myFile.setFileName(uuid+"."+file_end);
        myFile.setSourceName(fileName);
        myFile.setFilePath(fileServerPath);
        myFile.setFileType(f[f.length-1]);
        myFile.setCreateTime(new Date());
        myFile.setArchiveId(archiveId);
//                myFile.setCreater(userMapper.selectUserByToken(request.getHeader("Access-Token")).getUsername());
        myFile.setCreater("admin");
        return fileMapper.insert(myFile);
//                return ResultBean.createResultBySuccess(myFile.getFilePath());

    }

    @Override
    public List<InfoFile> selectAllFile() {
        return fileMapper.selectByExample(null);
    }


}
